﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using CoreShop.App;
using CoreShop.Library;
using CoreShop.Services;
using System.Windows.Forms;
using CoreShop.Model;
using CoreShop.PrintService;
using SqlSugar;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using Yitter.IdGenerator;
using SqlSugar.Extensions;
using Sunny.UI;
using FastMember;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using CoreShop.Model.FromBody;
using Sunny.UI.Win32;
using System.Diagnostics.Metrics;
using CoreShop.App.Payment.SxfPay;

namespace CoreShop.App
{
    public class PaymentUtil
    {
        public static bool SetPaymentMobile(corecmsfoodorderpayment orderpayment)
        {
            if (AppConstVars.payaccount == null)
            {
                UIMessageBox.Show("未绑定移动支付参数，请联系软件服务商开通。", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            switch (AppConstVars.systemsetup.mobilePayType)
            {
                case (int)GlobalEnumVars.MobilePayType.Nothing:
                    if (AppConstVars.systemsetup.sxfMno == "8888") return true;
                    UIMessageBox.Show("未绑定移动支付，请联系软件服务商开通。", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                    return false;
                case (int)GlobalEnumVars.MobilePayType.SXF: //随行付
                    if (AppConstVars.systemsetup.sxfMno.IsNullOrEmpty())
                    {
                        UIMessageBox.Show("未绑定移动支付，请联系软件服务商开通。", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                        return false;
                    }
                    frmMobilePay fp = new frmMobilePay(orderpayment);
                    fp.ShowDialog();
                    if (fp.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        return true;
                    }
                    break;                
                default:
                    break;
            }            
            return false;
        }

        public static string GetPayItemName(int index)
        {
            return "payItemName" + index.ToString();
        }

        /// <summary>
        ///  移动支付退款
        /// </summary>
        /// <param name="orderpayment"></param>
        /// <returns></returns>
        public static bool SetRefundMobilePay(OrderPaymentPost orderpayment,ref string retMsg)
        {
            if (AppConstVars.payaccount == null)
            {
                UIMessageBox.Show("未绑定移动支付参数，请联系软件服务商开通。", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }                

            switch (AppConstVars.systemsetup.mobilePayType)
            {
                case (int)GlobalEnumVars.MobilePayType.Nothing:
                    if (AppConstVars.systemsetup.sxfMno == "8888") return true;
                    UIMessageBox.Show("未绑定移动支付，请联系软件服务商开通。", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                    return false;
                case (int)GlobalEnumVars.MobilePayType.SXF: //随行付               
                    SxfPay sxfPay = new SxfPay(AppConstVars.systemsetup, AppConstVars.payaccount);
                    string orderNo = orderpayment.orderId.ToString() + "Rt" + DateTime.Now.ToString("hhmmss");
                    return sxfPay.SetRefund(orderNo, orderpayment.orderId.ToString(), orderpayment.payAmount, ref retMsg);
                  default:
                    break;
            }
            return false;
        }

        public static VipCouponInfoModel GetCouponUsage(List<corecmsfoodordering> orderingDetail, 
            corecmsuser member, List<corecmsfoodorderpayment> orderpayments)
        {
            VipCouponInfoModel foodcouponuser = null;
            var couponPayments = PaymentUtil.GetCouponsFormPayments(orderpayments);
            var frm = new frmCouponUsage(orderingDetail,member, couponPayments);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foodcouponuser = frm.foodcouponuser;
            }
            frm.Dispose();
            return foodcouponuser;
        }

        public static corecmsfoodorderpayment GetUserCouponPay(List<corecmsfoodordering> orderingDetail ,corecmsuser member, corecmsfoodorderingmaster orderingMaster)
        {
            corecmsfoodorderpayment paymentModel = null;
            var frm = new frmCouponSel(member, orderingDetail, orderingMaster);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                VipCouponInfoModel vipCouponInfo = frm.vipCouponInfo;
                List<VipCouponInfoModel> vipCoupons = new List<VipCouponInfoModel>{ vipCouponInfo };
                paymentModel = new corecmsfoodorderpayment();
                paymentModel.payCode = vipCouponInfo.couponNo;
                switch (CommFunc.ConvertToInt32(vipCouponInfo.couponTypeId))
                {
                    case (int)GlobalEnumVars.CouponType.FullForCut:
                        paymentModel.payAmount = vipCouponInfo.amount;
                        break;
                    case (int)GlobalEnumVars.CouponType.CouponDiscount:
                        //打折券
                        decimal dAllAmount = orderingMaster.sumAmount + orderingMaster.sumFeedAmount + orderingMaster.sumTaskAmount;
                        paymentModel.payAmount = dAllAmount * (1-vipCouponInfo.amount);
                        break;
                    default:
                        paymentModel.payAmount = vipCouponInfo.amount;
                        break;
                }
                paymentModel.couponList = vipCoupons;
            }
            frm.Dispose();
            return paymentModel;
        }

        public static List<VipCouponInfoModel> GetCouponsFormPayments(List<corecmsfoodorderpayment> orderpayments)
        {
            var couponPayments = new List<VipCouponInfoModel>();//支付的券明细
            var couponPays = orderpayments.Where(p => p.paymentId == (int)GlobalEnumVars.Payment.coupon).ToList();
            if ((couponPays == null) || (couponPays.Count == 0)) return null;              
            foreach (var item in couponPays)
            {
                var couponList = item.couponList; 
                if (couponList == null) continue;
                couponPayments.AddRange(couponList);
            }
            return couponPayments;
        }   
    }
}
